package com.hsy.sunflowerbao.home.ui

import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.view.View
import com.flux.action.RxStoreChange
import com.flux.utils.GlideUtil
import com.flux.utils.GsonUtil
import com.hsy.sunflowerbao.R
import com.hsy.sunflowerbao.base.action.ActionType
import com.hsy.sunflowerbao.base.data.UserData
import com.hsy.sunflowerbao.base.ui.activity.BaseActivityWithToolbar
import com.hsy.sunflowerbao.home.action.HomeActionCreator
import com.hsy.sunflowerbao.home.store.ScoreStore
import com.hsy.sunflowerbao.utils.CommonUtils
import kotlinx.android.synthetic.main.activity_score.*

/**
 * Created on 2018/4/9.
 *
 * @author Yang
 */
class ScoreActivity : BaseActivityWithToolbar<ScoreStore>() {

    /**
     * 事件产生器
     */
    private val creator = HomeActionCreator(dispatcher, manager)
    /**
     * 数据缓存器
     */
    override var store = ScoreStore(dispatcher)
    /**
     * 专家库适配器
     */
    private lateinit var adapter: TagAdapter

    private lateinit var uid: String

    val tags: MutableList<String> = mutableListOf()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_score)
        setTitle(R.string.title_detail)

        val user = GsonUtil.fromJson(activityStartExtra!![0], UserData::class.java)
        uid = user.id
        GlideUtil.loadCircle(user.avatar, userAvatar, R.drawable.default_avatar, activity = this)
        userName.text = getString(R.string.txt_home_user_name, user.nick_name)
        userPhone.text = getString(R.string.txt_home_user_phone, user.user_name)
        recycle.layoutManager = GridLayoutManager(recycle.context, 3)

        finish.setOnClickListener { creator.sendScore(uid, score.text.toString(), stringListToString(tags)) }

        if (user.tags.isNotEmpty()) when (user.tags.size) {
            1 -> {
                tag1.text = user.tags[0].name
                tag2.visibility = View.GONE
                tag3.visibility = View.GONE
                tag4.visibility = View.GONE
                tagMore.visibility = View.GONE
            }
            2 -> {
                tag1.text = user.tags[0].name
                tag2.text = user.tags[1].name
                tag3.visibility = View.GONE
                tag4.visibility = View.GONE
                tagMore.visibility = View.GONE
            }
            3 -> {
                tag1.text = user.tags[0].name
                tag2.text = user.tags[1].name
                tag3.text = user.tags[2].name
                tag4.visibility = View.GONE
                tagMore.visibility = View.GONE
            }
            4 -> {
                tag1.text = user.tags[0].name
                tag2.text = user.tags[1].name
                tag3.text = user.tags[2].name
                tag4.text = user.tags[3].name
                tagMore.visibility = View.GONE
            }
            else -> {
                tag1.text = user.tags[0].name
                tag2.text = user.tags[1].name
                tag3.text = user.tags[2].name
                tag4.text = user.tags[3].name
            }
        }

        CommonUtils.showProgressDialog(this)
        creator.getTags()
    }

    /**
     * 数据刷新响应
     * @param change 需要响应的改变
     */
    override fun onRxStoreChanged(change: RxStoreChange) {
        super.onRxStoreChanged(change)
        if (change.storeId == store.tag) when (change.type) {
            ActionType.GET_TAGS -> {
                CommonUtils.hideProgressDialog()
                adapter = TagAdapter(this, store.tags)
                recycle.adapter = adapter
            }
            ActionType.SEND_SCORE -> finish(RESULT_OK)
        }
    }
}